﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sys.WebRes.Data;
using System.IO;
using System.Drawing;

namespace Sys.WebRes.Publish.Controllers
{
    public class FileController : Controller
    {
        /// <summary>
        /// 根据内部路径输出文件字节流
        /// 防止盗链
        /// </summary>
        /// <param name="path">
        /// Picture File/Pic/xxxx.jpg?w=100&h=100
        /// </param>
        /// <returns></returns>
        public ActionResult Index(string type, string folder, string fileName, string small = "noSmall")
        {
            var uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SysConfig.UploadFolder);

            var path = string.Format("{0}/{1}/{2}", type, folder, fileName);

            if (SysConfig.UploadPictureFolder.Equals(type))
            {
                var fullPath = Path.Combine(uploadFolder, path);
                //非缩略图
                if ("noSmall".Equals(small))
                {
                    return System.IO.File.Exists(fullPath)? File(fullPath, "image/jpeg")
                        : File(Path.Combine(uploadFolder, SysConfig.DefaultPicture), "image/jpeg");
                }
                else //缩略图
                {
                    var sPath = string.Format("{0}/{1}/small-{2}", type, folder, fileName);
                    var sFullPath = Path.Combine(uploadFolder, sPath);
                    if (System.IO.File.Exists(sFullPath))
                    {
                        return File(sFullPath, "image/jpeg");
                    }
                    else if (System.IO.File.Exists(fullPath))
                    {
                        CreateSmallPicture(fullPath, sFullPath);
                        return File(sFullPath, "image/jpeg");
                    }
                    else
                    {
                        return File(Path.Combine(uploadFolder, SysConfig.DefaultPicture), "image/jpeg");
                    }

                }
            }
            else if (SysConfig.UploadMediaFolder.Equals(type))
            {
                return File(Path.Combine(uploadFolder, SysConfig.DefaultPicture), "image/jpeg");
            }
            else //文件
            {
                return File(Path.Combine(uploadFolder, SysConfig.DefaultPicture), "image/jpeg");
            }
        }

        /// <summary>
        /// 创建缩略图
        /// </summary>
        /// <param name="picPath"></param>
        private void CreateSmallPicture(string fileName, string sFileName)
        {
            using (Image image = Image.FromFile(fileName))
            {
                if (image.Width <= 100)
                {
                    image.Save(sFileName);
                    return;
                }

                using (Image sImage = new Bitmap(image, 100, image.Height * 100 / image.Width))
                {
                    sImage.Save(sFileName);
                }
            }
        }
    }
}
